package xcmg.device.web.warehouse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.dao.entity.warehouse.DepartmentWarehouseRelationDO;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.warehouse.AddDepartmentWarehouseVO;
import xcmg.device.service.vo.warehouse.DepartmentWarehouseRelationVO;
import xcmg.device.service.warehouse.DepartmentWarehouseRelationService;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by wrg on 2019/5/20.
 */
@RestController
@RequestMapping("/departmentWarehouse")
@Api("维护仓库控制入口")
public class DepartmentWarehouseRelationController extends BasicController {
    @Autowired
    private DepartmentWarehouseRelationService relationService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "部门库存关系")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody AddDepartmentWarehouseVO addVO) throws Exception {
        try {
            if (addVO == null ||
                    null == addVO.getDepartmentId() ||
                    null == addVO.getWarehouseRelationVOList() ||
                    addVO.getWarehouseRelationVOList().isEmpty()) {
                return actionResult(ErrorCode.IllegalArument);
            }

            addVO.setOrgId(getOrgId());

            ErrorCode ret = relationService.add(addVO);
            return actionResult(ret);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    /**
    *@Description 仓库配置v2版接口，配置仓库库存控制，主库区次库去配置
    *@Author wzy
    *@Date 2020/8/24
    *@Time 14:11
    */
    @PostMapping(value = "/addV2")
    @ApiOperation(value = "部门库存关系")
    @FastMappingInfo(needLogin = true)
    public ActionResult addV2(@RequestBody AddDepartmentWarehouseVO addVO) throws Exception {
        try {
            if (addVO == null ||
                    null == addVO.getDepartmentId() ||
                    null == addVO.getWarehouseRelationVOList() ||
                    addVO.getWarehouseRelationVOList().isEmpty()) {
                return actionResult(ErrorCode.IllegalArument);
            }
            String orgId = getOrgId();
            //遍历数据，把同一个仓库的三个控制进行合并，拼为一条数据，与数据库的结构对应
            List<DepartmentWarehouseRelationDO> departmentWarehouseRelations = assembleSaveData(addVO);
            ErrorCode ret = relationService.add(departmentWarehouseRelations,orgId,addVO.getDepartmentId());
            return actionResult(ret);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    //拼接仓库三种控制字段的值
    private List<DepartmentWarehouseRelationDO> assembleSaveData(AddDepartmentWarehouseVO addVO) {
        String departmentId = addVO.getDepartmentId();
        String orgId = addVO.getOrgId();
        List<DepartmentWarehouseRelationVO> warehouseRelationVOList = addVO.getWarehouseRelationVOList();
        List<DepartmentWarehouseRelationVO> majorControls = addVO.getMajorControls();
        List<DepartmentWarehouseRelationVO> minorControl = addVO.getMinorControls();
        //按照仓库名称，把三个控制中的数据进行拼接
        Map<String, DepartmentWarehouseRelationDO> wareHouseControlMap = new HashMap<>();
        //拼接仓库库存控制字段
        assembleWareHouseControl(warehouseRelationVOList,wareHouseControlMap,orgId,departmentId);
        //拼接单据主库区控制字段
        assembleMajorControl(majorControls,wareHouseControlMap,orgId,departmentId);
        //拼接单据次库区控制字段
        assembleMinorControl(minorControl,wareHouseControlMap,orgId,departmentId);
        return wareHouseControlMap.values().stream().collect(Collectors.toList());
    }

    //拼接次库区控制值
    private void assembleMinorControl(List<DepartmentWarehouseRelationVO> minorControl, Map<String, DepartmentWarehouseRelationDO> wareHouseControlMap, String orgId, String departmentId) {
        for(DepartmentWarehouseRelationVO warehouseVO : minorControl){
            String warehouseId = warehouseVO.getWarehouseId();
            String warehouseCode = warehouseVO.getWarehouseCode();
            boolean check = warehouseVO.isCheck();
            if(!check){
                continue;
            }
            //如果check为true表示主库区字段需要赋值为1
            if(wareHouseControlMap.containsKey(warehouseId)){
                wareHouseControlMap.get(warehouseId).setDocumentMinorControl(1);
            }else{
                DepartmentWarehouseRelationDO relationDO = new DepartmentWarehouseRelationDO();
                relationDO.setDocumentMinorControl(1);
                relationDO.setWarehouseId(warehouseId);
                relationDO.setWarehouseCode(warehouseCode);
                relationDO.setOrgId(orgId);
                relationDO.setDepartmentId(departmentId);
                relationDO.setIsDel((short)0);
                wareHouseControlMap.put(warehouseId,relationDO);
            }
        }
    }

    //拼接主库区控制值
    private void assembleMajorControl(List<DepartmentWarehouseRelationVO> majorControls, Map<String, DepartmentWarehouseRelationDO> wareHouseControlMap, String orgId, String departmentId) {
        for(DepartmentWarehouseRelationVO warehouseVO : majorControls){
            String warehouseId = warehouseVO.getWarehouseId();
            String warehouseCode = warehouseVO.getWarehouseCode();
            boolean check = warehouseVO.isCheck();
            if(!check){
                continue;
            }
            //如果check为true表示主库区字段需要赋值为1
            if(wareHouseControlMap.containsKey(warehouseId)){
                wareHouseControlMap.get(warehouseId).setDocumnetMajorControl(1);
            }else{
                DepartmentWarehouseRelationDO relationDO = new DepartmentWarehouseRelationDO();
                relationDO.setDocumnetMajorControl(1);
                relationDO.setWarehouseId(warehouseId);
                relationDO.setWarehouseCode(warehouseCode);
                relationDO.setOrgId(orgId);
                relationDO.setDepartmentId(departmentId);
                relationDO.setIsDel((short)0);
                wareHouseControlMap.put(warehouseId,relationDO);
            }
        }
    }

    /**
    *@Description 拼接仓库库存控制配置
    *@Author wzy
    *@Date 2020/8/24
    *@Time 15:20
    */
    private void assembleWareHouseControl(List<DepartmentWarehouseRelationVO> warehouseRelationVOList,Map<String, DepartmentWarehouseRelationDO> wareHouseControlMap,
                                          String orgId,String departmentId){
        for(DepartmentWarehouseRelationVO warehouseVO : warehouseRelationVOList){
            String warehouseId = warehouseVO.getWarehouseId();
            String warehouseCode = warehouseVO.getWarehouseCode();
            boolean check = warehouseVO.isCheck();
            if(!check){
                continue;
            }
            //如果check为true表示仓库库存字段需要赋值为1
            if(wareHouseControlMap.containsKey(warehouseId)){
                wareHouseControlMap.get(warehouseId).setWarehouseInvertoryControl(1);
            }else{
                DepartmentWarehouseRelationDO relationDO = new DepartmentWarehouseRelationDO();
                relationDO.setWarehouseInvertoryControl(1);
                relationDO.setWarehouseId(warehouseId);
                relationDO.setWarehouseCode(warehouseCode);
                relationDO.setOrgId(orgId);
                relationDO.setDepartmentId(departmentId);
                relationDO.setIsDel((short)0);
                wareHouseControlMap.put(warehouseId,relationDO);
            }
        }
    }

    @GetMapping(value = "/addIsEnable")
    @ApiOperation(value = "新增是否启用仓库开关,isEnable=0:禁用，1：启用")
    @FastMappingInfo(needLogin = true)
    public ActionResult addIsEnable(@RequestParam("isEnable") Integer isEnable,@RequestParam("departmentId") String departmentId) throws Exception {
        if (null == isEnable) {
            return actionResult(ErrorCode.IllegalArument);
        }

        ErrorCode ret = relationService.addIsEnable(isEnable,departmentId,getOrgId());
        return actionResult(ret);
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "部门库存关系")
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids) throws Exception {
        if (ids == null || ids.length == 0) {
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret = relationService.del(Arrays.asList(ids));
        return actionResult(ret);
    }

    @GetMapping(value = "/queryWarehouseList")
    @ApiOperation(value = "查询已关联的仓库列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult queryWarehouseList(@RequestParam("departmentId") String departmentId) throws Exception {
        return actionValueResult(relationService.queryWarehouseList(departmentId, getOrgId()));
    }

    @GetMapping(value = "/queryWarehouseListV2")
    @ApiOperation(value = "查询已关联的仓库列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult queryWarehouseListV2(@RequestParam("departmentId") String departmentId) throws Exception {
        String orgId = getOrgId();
        return actionValueResult(relationService.queryWarehouseListV2(departmentId, orgId));
    }

    @GetMapping(value = "/getIsEnableStatus")
    @ApiOperation(value = "获取开启状态,isEnable=0:禁用，1：启用")
    @FastMappingInfo(needLogin = true)
    public ActionResult getIsEnableStatus(@RequestParam("departmentId") String departmentId) throws Exception {
        if (null == departmentId) {
            return actionResult(ErrorCode.IllegalArument);
        }

        Integer isEnableStatus = relationService.getIsEnableStatus(departmentId, getOrgId());
        return actionValueResult(isEnableStatus);
    }

}
